/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/08/18
*/
#include "Label.h"
#include "../edui.h"

EDUI_NAMESPACE_BEGIN

Label::Label() : m_font(0), m_bAutoWidth(false), m_bAutoHeight(false), m_bTranslateText(false), m_textAlign(ALIGN_CENTER)
{
    m_text.Empty();
    m_textColor.Reset(0, 0, 0, 255);
}

Label::~Label()
{}

Label* Label::GetInterface()
{
    Label* label = new Label;
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[Label] new label : %d", label);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Add("Label", label);
#endif
    return label;
}

void Label::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[Label] delete label : %s, %d", this->GetName(), this);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Remove(this);
#endif
    delete this;
}

Label* Label::ToLabel(Control* ctrl)
{
    return dynamic_cast<Label*>(ctrl);
}

void Label::SetText(const char* text)
{
    if (0 == strcmp(text, m_text.GetString())) return;

    m_text = text;

    this->Invaldate();
}

const char* Label::GetText() const
{
    return m_text.GetString();
}

void Label::SetTranslateText(bool bTranslate)
{
    m_bTranslateText = bTranslate;
}
 
bool Label::IsTranslateText()
{
    return m_bTranslateText;
}

const char* Label::GetTranslateText()
{
    if (!m_bTranslateText) return m_text.GetString();
    return IEnginePtr->GetText(m_text.GetString(), 0);
}

IFont* Label::GetFont(bool bWindow) const
{
    if (!bWindow) return m_font;
    if (m_font) return m_font;
    return this->GetWindow()->GetFont();
}

bool Label::SetFont(IFont* font)
{
    if (!font) return false;
    m_font = font;
    return true;
}

AlignType Label::GetTextAlign()
{
    return m_textAlign;
}

void Label::SetTextAlign(AlignType align)
{
    m_textAlign = align;
}

Rect1 Label::GetTextPadding() const
{
    return m_textPadding;
}
    
bool Label::SetTextPadding(const Rect1* padding)
{
    if (!padding) return false;
    m_textPadding = *padding;
    return true;
}

void Label::SetAutoWidth(bool bauto)
{
    m_bAutoWidth = bauto;
}

bool Label::IsAutoWidth() const
{
    return m_bAutoWidth;
}

void Label::SetAutoHeight(bool bauto)
{
    m_bAutoHeight = bauto;
}

bool Label::IsAutoHeight() const
{
    return m_bAutoHeight;
}

Size Label::GetFixedSize() const
{
    if (!m_bAutoHeight && !m_bAutoWidth) return Size(m_rcFixed.w, m_rcFixed.h);

    int w = m_rcFixed.w;
    int h = m_rcFixed.h;

    IFont* font = this->GetFont();
    if (font){
        const char* text = m_text.GetString();
        if (m_bTranslateText) text = IEnginePtr->GetText(m_text.GetString(), 0);
        Size sz = font->GetSizeUTF8(text);
        if (m_bAutoWidth)  w = sz.w + m_rcPadding.l + m_rcPadding.r + m_textPadding.l + m_textPadding.r;
        if (m_bAutoHeight) h = sz.h + m_rcPadding.t + m_rcPadding.b + m_textPadding.t + m_textPadding.b;
    }
    
    return Size(w, h);
}

Rect Label::GetFixedRect() const
{
    Rect rc = m_rcFixed;
    Size sz = this->GetFixedSize();
    rc.w = sz.w;
    rc.h = sz.h;
    return rc;
}

Rect Label::GetTextAlignRect(Size* fontSize)
{
    Rect rcex = m_rc;
    if (m_rcFixed.w) rcex.w = m_rcFixed.w;
    if (m_rcFixed.h) rcex.h = m_rcFixed.h;
    // center
    Rect rc( rcex.x + (rcex.w - fontSize->w)/2, rcex.y + (rcex.h - fontSize->h)/2, fontSize->w, fontSize->h );
    switch (m_textAlign){
    case ALIGN_CENTER :
        break;
    case ALIGN_LEFT :
        rc.x = rcex.x;
        break;
    case ALIGN_LEFTTOP :
        rc.x = rcex.x;
        rc.y = rcex.y;
        break;
    case ALIGN_TOP :
        rc.y = rcex.y;
        break;
    case ALIGN_RIGHTTOP :
        rc.y = rcex.y;
        rc.x = rcex.x + rcex.w - fontSize->w;
        break;
    case ALIGN_RIGHT :
        rc.x = rcex.x + rcex.w - fontSize->w;
        break;
    case ALIGN_RIGHTBOTTOM :
        rc.x = rcex.x + rcex.w - fontSize->w;
        rc.y = rcex.y + rcex.h - fontSize->h;
        break;
    case ALIGN_BOTTOM :
        rc.y = rcex.y + rcex.h - fontSize->h;
        break;
    case ALIGN_LEFTBOTTOM :
        rc.x = rcex.x;
        rc.y = rcex.y + rcex.h - fontSize->h;
        break;
    }

    rc.x += m_textPadding.l;
    rc.y += m_textPadding.t;

    if (rc.x + rc.w > m_rc.x + m_rc.w - m_textPadding.r) rc.w = m_rc.x + m_rc.w - m_textPadding.r - rc.x;
    if (rc.y + rc.h > m_rc.y + m_rc.h - m_textPadding.b) rc.h = m_rc.y + m_rc.h - m_textPadding.b - rc.y;

    return rc;
}

void Label::RenderText(IRender* render, const Rect* renderrc)
{
    if (m_text.IsEmpty()) return;

    //get text
    const char* text = this->GetTranslateText();
    if (!text) return;

    //get font
    IFont* font = this->GetFont();
    if (!font) return;

    //render
    Size sz = font->GetSizeUTF8(text);
    Rect rc = this->GetTextAlignRect(&sz);
    render->DrawText(text, &rc, font, &m_textColor);
}

Color* Label::GetDestColor(const char* value)
{
    if (0 == strcmp(value, "text")){
        return &m_textColor;
    }
    return Control::GetDestColor(value);
}

void Label::SetAttribute(const char* name, const char* value)
{
    if (0 == strcmp(name, "font")){
        FontMgr* fontMgr = IEnginePtr->GetFontMgr();
        IFont* font = fontMgr->Get(value);
        if (font) this->SetFont(font);
    } else if (0 == strcmp(name, "fontid")){
        int id = -1;
        if (ParserInt(value, &id, -1) && (id >= 0)){
            FontMgr* fontMgr = IEnginePtr->GetFontMgr();
            IFont* font = fontMgr->Get(id);
            if (font) this->SetFont(font);
        }
    } else if (0 == strcmp(name, "text")){
        this->SetText(value);
    } else if (0 == strcmp(name, "textpadding")){
        Rect1 rc = ParserRect1(value);
        this->SetTextPadding(&rc);
    } else if (0 == strcmp(name, "align")){
        m_textAlign = ParserAlign(value);
    } else if (0 == strcmp(name, "translatetext")){
        m_bTranslateText = ParserBool(value, false);
    } else if (0 == strcmp(name, "autowidth")){
        m_bAutoWidth = ParserBool(value, false);
    } else if (0 == strcmp(name, "autoheight")){
        m_bAutoHeight = ParserBool(value, false);
    } else {
        Control::SetAttribute(name, value);
    }
}

EDUI_NAMESPACE_END
